package org.libl.dao.student.impl;

import org.libl.utils.JDBCPoolUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.libl.dao.student.StudentDao;
import org.libl.domain.Student;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    //1.创建模板类对象
    private JdbcTemplate jt = new JdbcTemplate(JDBCPoolUtils.getDs());
    public List<Student> selectAll(){
        List<Student> list = jt.query(
                "select * from tab_student",
                new BeanPropertyRowMapper<Student>(Student.class));
        return list;
    }

    public List<Student> findByPage(Integer currentPage, Integer pageSize){
        List<Student> list = jt.query(
                "select * from tab_student limit ?,?",
                new BeanPropertyRowMapper<Student>(Student.class),
                (currentPage-1)*pageSize,
                      pageSize
                   );
        return list;
    }

    public Number findTotalCount(){
        Number totalCount = jt.queryForObject("select count(sid) from tab_student", Number.class);
        return totalCount;
    }

    public int  save(String sname,String age,String email,String telephone,String addr,String sex){
        //1.编写sql
        String sql = "insert into tab_student(sname,age,email,telephone,addr,sex) values(?,?,?,?,?,?)";
        //2.调用更新方法
        int row = jt.update(sql,sname,age,email,telephone,addr,sex);

        return row;
    }

    public  int del (String sid){

        //1.编写sql
        String sql = "delete from tab_student where sid = ?";
        //2.调用更新方法
        int row = jt.update(sql,Integer.parseInt(sid));

        return row;
    }

    public int  edit(String sid,String sname,String age,String email,String telephone,String addr,String sex){
        //1.编写sql
        String sql = "update tab_student set sname = ? ,age=?,email=?,telephone=?,addr=?,sex=? where sid = ?";
        //2.调用更新方法
        int row = jt.update(sql,sname,age,email,telephone,addr,sex,Integer.parseInt(sid));
        return row;
    }
}
